package java_thinking.unit_10._10_7.Exercise;

import java_thinking.unit_10._10_1_2.Selector;

/**
 * @Description : 练习22 实现Sequence.java中的reverseSelector()方法
 * @author: cww
 * @DateTime: 2019/3/11 19:34
 */

class Sequence3 {
    private static Object[] objects = new Object[10];
    private int next;
    public Sequence3(int size){
        objects = new Object[size];
    }
    public void add(Object x){
        if (next<objects.length){
            objects[next++] = x;
        }
    }

    /**
     * 这个内部类可以用匿名内部类代替
     */
    private class ReverseSelector implements Selector {
        int i = objects.length - 1;
        @Override
        public boolean end() {
            return i<0;
        }

        /**
         * 获取Object的每个对象
         *
         * @return
         */
        @Override
        public Object current() {
            return objects[i];
        }

        @Override
        public void next() {
            if (i>=0){
                i--;
            }
        }
    }

    /**
     * 创建接口对象方法返回
     * @return
     */
    public Selector selector(){
        return new ReverseSelector();
    }

    /**
     * 代替上面的接口实现与创建接口对象
     * 但是注意：工厂生产的对象是唯一的
     *
     *
     */
    public static Selector selector = new Selector() {
        int i = objects.length - 1;
        @Override
        public boolean end() {
            return i<0;
        }

        /**
         * 获取Object的每个对象
         *
         * @return
         */
        @Override
        public Object current() {
            return objects[i];
        }

        @Override
        public void next() {
            if (i>=0){
                i--;
            }
        }
    };

    /**
     * 非静态内部类
     * @return
     */
    public Selector getSelector1(){
        return new Selector() {
            int i = objects.length - 1;
            @Override
            public boolean end() {
                return i<0;
            }

            /**
             * 获取Object的每个对象
             *
             * @return
             */
            @Override
            public Object current() {
                return objects[i];
            }

            @Override
            public void next() {
                if (i>=0){
                    i--;
                }
            }
        };
    }

    private class SequenceSelector implements Selector {
        private int i;
        @Override
        public boolean end() {
            return i == objects.length;
        }

        /**
         * 获取Object的每个对象
         *
         * @return
         */
        @Override
        public Object current() {
            return objects[i];
        }

        @Override
        public void next() {
            if (i<objects.length){
                i++;
            }
        }
    }
    public Selector reverseSelector(){
        return new ReverseSelector();
    }
}

class E12_GetSelector{
    public static void main(String[] args) {
        Sequence3 sequence3 = new Sequence3(10);
        for (int i = 0;i<10;i++){
            sequence3.add(i);
        }
        Selector selector = sequence3.reverseSelector();
        Selector selector1 = sequence3.selector();
        while (!selector1.end()){
            System.out.println(selector1.current());
            selector1.next();
        }
    }
}
